package com.ruoyi.web.controller.guarantee;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.guarantee.domain.BAgreement;
import com.ruoyi.guarantee.domain.BAgreementLog;
import com.ruoyi.guarantee.domain.BLetter;
import com.ruoyi.guarantee.service.IBAgreementLogService;
import com.ruoyi.guarantee.service.IBAgreementService;
import com.ruoyi.guarantee.service.IBLetterService;
import com.ruoyi.guarantee.service.IDiffAgreementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.*;
import java.io.IOException;
import java.util.List;

/********************************************************************************
 ** author： zhangtianhe
 ** date：2024年12月13日 16:13
 ** description&desc：
 *********************************************************************************/
@RestController
@RequestMapping("/guarantee/lvyue")
public class BLvyueController  extends BaseController {
    @Autowired
    private IBAgreementService bAgreementService;

    @Autowired
    private IBLetterService bLetterService;
    @Autowired
    private IDiffAgreementService diffAgreementService;
    @Autowired
    private IBAgreementLogService agreementLogService;

    /**
     * 查询履约保函列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BAgreement bAgreement)
    {
        bAgreement.setUserId(getUserId());
        bAgreement.setStatus(0L);
        startPage();
        List<BAgreement> list = bAgreementService.selectBAgreementList(bAgreement);
        return getDataTable(list);
    }

    /**
     * 导出履约保函列表
     */
    @Log(title = "履约保函", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BAgreement bAgreement)
    {
        List<BAgreement> list = bAgreementService.selectBAgreementList(bAgreement);
        ExcelUtil<BAgreement> util = new ExcelUtil<BAgreement>(BAgreement.class);
        util.exportExcel(response, list, "履约保函数据");
    }

    /**
     * 获取履约保函详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        BAgreement bAgreement = bAgreementService.selectBAgreementById(id);
        if(bAgreement.getXmNameLot()!=null){
            bAgreement.setXmNameLotList(bAgreement.getXmNameLot().split(","));
        }

        return success(bAgreement);
    }

    /**
     * 新增履约保函
     */
    @Log(title = "履约保函", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BAgreement bAgreement)
    {
        String xmNameLot = arrayToString(bAgreement.getXmNameLotList(), ", ");
        bAgreement.setXmNameLot(xmNameLot);
        bAgreement.setStatus(0L);
        bAgreement.setUserId(getUserId());
        bAgreement.setCreateBy(getUsername());
        return toAjax(bAgreementService.insertBAgreement(bAgreement));
    }


    public static String arrayToString(String[] array, String delimiter) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                builder.append(delimiter);
            }
            builder.append(array[i]);
        }
        return builder.toString();
    }

    /**
     * 修改履约保函
     */
    @Log(title = "履约保函", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BAgreement bAgreement)
    {
        return toAjax(bAgreementService.updateBAgreement(bAgreement));
    }

    /**
     * 删除履约保函
     */
    @Log(title = "履约保函", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bAgreementService.deleteBAgreementByIds(ids));
    }

    public static void main1(String[] args) {
        // 获取当前系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        // 复制文本到剪贴板
        String textToCopy = "这是要复制的文本.\n Itincludes a line break.";
        StringSelection stringSelection = new StringSelection(textToCopy);
        clipboard.setContents(stringSelection, null);

        // 从剪贴板粘贴文本
        JOptionPane.showMessageDialog(null, "文本已复制到剪贴板");
        String textPasted = null;
        try {
            textPasted = (String) clipboard.getData(java.awt.datatransfer.DataFlavor.stringFlavor);
        } catch (UnsupportedFlavorException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        JOptionPane.showMessageDialog(null, "粘贴的文本: " + textPasted);
    }

    public static void main(String[] args) {
        // 获取系统剪贴板
        Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();

        // 清空剪贴板
        clipboard.setContents(new Transferable() {
            public boolean isDataFlavorSupported(DataFlavor flavor) {
                return false;
            }
            public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
                return "";
            }
            public DataFlavor[] getTransferDataFlavors() {
                return new DataFlavor[0];
            }
        }, null);

        // 复制文本到剪贴板
        String textToCopy = "Hello, World!";
        Transferable trans = new StringSelection(textToCopy);
        clipboard.setContents(trans, null);
    }

    /**
     * 履约保函提交
     */
    @Log(title = "履约保函", businessType = BusinessType.DELETE)
    @PostMapping("/tijiao")
    public AjaxResult tijiao( @RequestBody BAgreement agreement)
    {
        BAgreement bAgreement = bAgreementService.selectBAgreementById(agreement.getId());
        if (bAgreement == null) {
            return AjaxResult.error("该保函不存在");
        }
        Long status = bAgreement.getStatus();
        if(status == 0||status == -1){
            bAgreement.setStatus(2L);
            return toAjax(bAgreementService.updateBAgreement(bAgreement));
        }
        return AjaxResult.error("该保函已提交");
    }

    /**
     * 查询履约保函列表
     */
    @PreAuthorize("@ss.hasPermi('guarantee:lvyue:list')")
    @GetMapping("/listLvyue")
    public TableDataInfo listLvyue(BAgreement bAgreement)
    {
        startPage();
        List<BAgreement> list = bAgreementService.selectHistoryBAgreementList(bAgreement);
        return getDataTable(list);
    }

    /**
     * 获取所有保函机构及负责人
     */
    @GetMapping("/getLetterList")
    public AjaxResult getLetterList()
    {
        List<BLetter> letterList = bLetterService.selectBLetterList(new BLetter());
        return AjaxResult.success(letterList);
    }

    /**
     * 获取所有对接人
     */
    @GetMapping("/getDuiije")
    public AjaxResult getDuiije()
    {
        List<BLetter> letterList = bLetterService.selectDuiije();
        return AjaxResult.success(letterList);
    }

    /**
     * 获取所有对接人
     */
    @GetMapping("/getLetter")
    public AjaxResult getDuiije(String name)
    {
        if (StringUtils.isNotEmpty(name)){
            BLetter bLetter = new BLetter();
            bLetter.setName(name);
            List<BLetter> letterList = bLetterService.selectBLetterByName(bLetter);
            return AjaxResult.success(letterList);
        }else {
            return AjaxResult.error();
        }

    }
    /**
     * 修改履约保函
     */
    @Log(title = "履约保函", businessType = BusinessType.UPDATE)
    @PostMapping("/updateLvyue")
    public AjaxResult updateLvyue(@RequestBody BAgreement bAgreement)
    {
        BAgreement oldBagreement = bAgreementService.selectBAgreementById(bAgreement.getId());
        return toAjax(diffAgreementService.diffAgreement(oldBagreement,bAgreement,getUsername()));
    }

}
